package cn.pfund.controller.admin;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.pfund.controller.BaseController;
import cn.pfund.entity.NewsType;
import cn.pfund.service.NewsTypeService;
import cn.pfund.util.Format;
import cn.pfund.util.Pagination;
import cn.pfund.util.ParamBean;

/**
 * 新闻类别
 * @author zhaominglei qq:351024274，专注于网站、app和游戏开发。
 * @date 2017年1月20日
 */
@Controller("adminNewsTypeController")
@RequestMapping("/admin/newsType")
public class NewsTypeController extends BaseController {
    @Resource(name="newsTypeService")
    private NewsTypeService newsTypeService;
    
    @RequestMapping(value = "/list", method=RequestMethod.GET)
	public String list(ParamBean paramBean, HttpServletRequest request, HttpServletResponse response, Model model){
		paramBean.setUrl(getBasePath(request) + "admin/newsType/list.shtml");
		int p = (Format.null2Zero(request.getParameter("p")) == 0) ? 1 : Format.null2Zero(request.getParameter("p"));
		paramBean.setPage(p);
		Pagination<NewsType> pagination = newsTypeService.getPagination(paramBean);
		request.setAttribute("pagination", pagination);
		return "admin/newsType/list";
	}
	
	@RequestMapping(value = "/add",method=RequestMethod.GET)
	public String add(HttpServletRequest request, HttpServletResponse response, Model model){
		return "admin/newsType/add";
	}
	
	@RequestMapping(value = "/save", method=RequestMethod.POST)
	public String save(NewsType newsType, HttpServletRequest request, HttpServletResponse response, Model model){
		newsTypeService.save(newsType);
		return "redirect:/admin/newsType/list.shtml";
	}
	
	@RequestMapping(value = "/edit",method=RequestMethod.GET)
	public String edit(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		request.setAttribute("newsType", newsTypeService.getById(NewsType.class, id));
		return "admin/newsType/edit";
	}
	
	@RequestMapping(value = "/update", method=RequestMethod.POST)
	public String update(NewsType newsType, HttpServletRequest request, HttpServletResponse response, Model model){
		NewsType pNewsType = newsTypeService.getById(NewsType.class, newsType.getId());
		if (pNewsType == null) {
			return ERROR_VIEW;
		}
		newsTypeService.update(newsType);
		return "redirect:/admin/newsType/list.shtml";
	}
	
	@RequestMapping(value = "/view",method=RequestMethod.GET)
	public String view(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		request.setAttribute("newsType", newsTypeService.getById(NewsType.class, id));
		return "admin/newsType/view";
	}

    @RequestMapping(value = "/delete",method=RequestMethod.GET)
    public String delete(String ids, HttpServletRequest request, HttpServletResponse response, Model model) {
        String[] idArray = ids.split(","); //支持批删 id逗号隔开
        for(String id:idArray){
        	NewsType newsType = newsTypeService.getById(NewsType.class, Long.valueOf(id));
        	if (newsType == null) continue;
        	newsTypeService.delete(newsType);
        }
        return "redirect:/admin/newsType/list.shtml";
    }	
}